package com.hello.booksale.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hello.booksale.entity.BookEntity;
import com.hello.booksale.entity.CartEntity;
import com.hello.booksale.entity.OrderEntity;
import com.hello.booksale.service.BookService;
import com.hello.booksale.service.CartService;
import com.hello.booksale.service.OrderService;
import com.hello.booksale.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private BookService bookService;

    @Resource
    private CartService cartService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private OrderService orderService;
    @GetMapping("/main")
    public String main() {
        return "forward:/user/main/1";
    }

    @GetMapping("/main/{pageNum}")
    public String main(@PathVariable(name = "pageNum") int pageNum, Model m) {
        PageHelper.startPage(pageNum, 5);
        List<BookEntity> list = bookService.selectBook();
        PageInfo<BookEntity> pageInfo = new PageInfo<>(list, 5);
        m.addAttribute("pageInfo", pageInfo);
        m.addAttribute("bookList", list);
        return "/user/main";
    }
    @PostMapping("/select")
    public String select(@RequestParam("bookName") String bookName,Model m){
        PageHelper.startPage(1, 5);
        List<BookEntity>list=bookService.selectBookByBookName(bookName);
        PageInfo<BookEntity> pageInfo = new PageInfo<>(list, 5);
        m.addAttribute("pageInfo", pageInfo);
        m.addAttribute("bookList",list);
        return "/user/main";
    }
    @GetMapping("/book/{bookId}")
    public String detail(@PathVariable("bookId") int bookId , HttpServletRequest request){
        BookEntity bookEntity=bookService.selectBookById(bookId);
        request.setAttribute("bookEntity",bookEntity);
        return "user/detail";
    }
    @GetMapping("/cart")
    public String cart(@CookieValue("token") String token,HttpServletRequest request) {
        String name=jwtUtil.getUsername(token);
        List<CartEntity> list=cartService.selectCartByUserName(name);
        log.error(list.toString());
        List<String>nameList=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            nameList.add(list.get(i).getBookName());
        }
        request.setAttribute("cartList",list);
        request.setAttribute("nameList",nameList);
        return "/user/cart";
    }
    @GetMapping("/cart/{bookId}/{num}")
    public String goCart(@PathVariable("bookId") int bookId,@PathVariable("num")int num,@CookieValue("token") String token){
        String name=jwtUtil.getUsername(token);
        BookEntity bookEntity=bookService.selectBookById(bookId);
        CartEntity cartEntity=new CartEntity(name,bookId,num,bookEntity.getBookUrl(),bookEntity.getBookName(),bookEntity.getBookDetail(),bookEntity.getBookAuthor(),bookEntity.getBookPrice()*num,String.valueOf(System.currentTimeMillis()));
        cartService.insertCart(cartEntity);
        return "/user/cart";
    }

    @DeleteMapping("/cart/{bookId}/{cartTime}")
    public String deleteOrder(@PathVariable("bookId") int bookId,@PathVariable("cartTime") String cartTime){
        cartService.deleteCartByBookId(bookId,cartTime);
        return "user/cart";
    }
    @GetMapping("/order/{bookName}")
    public String order(@PathVariable ("bookName") String bookName,@CookieValue("token") String token, HttpServletRequest request) {
        String name=jwtUtil.getUsername(token);
        String[]stringName=bookName.split(",");
        List<BookEntity>list=new ArrayList<>();
        for (String s : stringName) {
            list = bookService.selectBookByBookName(s);
            System.out.println(list);
            OrderEntity orderEntity = new OrderEntity(name, 1, list.get(0).getBookUrl(), s, list.get(0).getBookPrice(), list.get(0).getBookAuthor());
            orderService.insertOrder(orderEntity);
        }
        List<OrderEntity>orderList=orderService.selectOrder(name);
        request.setAttribute("orderList",orderList);
        System.out.println(orderList.toString());
        if(!bookName.equals("")){
            cartService.deleteCartByUserName(name);
        }
        return "/user/order";
    }


    @GetMapping("/order")
    public String goOrder(@CookieValue("token") String token,HttpServletRequest request){
        String name=jwtUtil.getUsername(token);
        List<OrderEntity>orderList=orderService.selectOrder(name);
        request.setAttribute("orderList",orderList);
        return "/user/order";
    }
    @GetMapping("/buyOrder/{bookId}/{num}")
    public String buy(@PathVariable("bookId") int bookId,@PathVariable("num") int num,@CookieValue("token") String token){
        String name=jwtUtil.getUsername(token);
        BookEntity bookEntity=bookService.selectBookById(bookId);
        OrderEntity orderEntity=new OrderEntity(name,num,bookEntity.getBookUrl(),bookEntity.getBookName(),bookEntity.getBookPrice()*num,bookEntity.getBookAuthor());
        orderService.insertOrder(orderEntity);
        return "/user/order";
    }

}